package br.com.j7.jdesk.ui.bean;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import br.com.j7.jdesk.persistence.entity.Enterprises;
import br.com.j7.jdesk.persistence.entity.Users;
import br.com.j7.jdesk.persistence.entity.UsersGroups;
import br.com.j7.jdesk.persistence.repository.EnterpriseRepository;
import br.com.j7.jdesk.persistence.repository.UserGroupRepository;
import br.com.j7.jdesk.persistence.repository.UserRepository;
import br.com.j7.jdesk.persistence.repository.impl.EnterpriseRepositoryImpl;
import br.com.j7.jdesk.persistence.repository.impl.UserGroupRepositoryImpl;
import br.com.j7.jdesk.persistence.repository.impl.UserRepositoryImpl;

/**
 * 
 * 
 * @author Edivando Alves
 *
 */
@ManagedBean
@RequestScoped
public class EnterpriseBean extends WebContext implements Bean{

	private Enterprises enterprise = new Enterprises();
	private UsersGroups userGroup  = new UsersGroups();
	private Users user 			   = new Users();
	
	private List<Enterprises> enterprises;
	
	
	private EnterpriseRepository repository;
	private UserGroupRepository userGroupRepository;
	private UserRepository userRepository;
	
	public EnterpriseBean() {
		repository 			= new EnterpriseRepositoryImpl();
		userGroupRepository = new UserGroupRepositoryImpl();
		userRepository 		= new UserRepositoryImpl();
	}
	
	@Override
	public String adicionar() {
		try {
			boolean action = false;
			if(enterprise.getIdEnterprise() == null || enterprise.getIdEnterprise().equals(0)){
				enterprise.setIdEnterprise(null);
				action = true;
			}
			userGroup.setEnterprise(enterprise);

			user.setUserGroup(userGroup);
			
			repository.addEnterprise(enterprise, userGroup, user);
			if( action ){
				addMessage("ui.mobile.enterprise.add", enterprise.getCdEnterprise());
			}else{
				addMessage("ui.mobile.enterprise.update", enterprise.getCdEnterprise());
			}
			enterprise = new Enterprises();
			enterprises = null;
			userGroup = null;
			user = null;
			return "enterprise";
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}

	@Override
	public String remover() {
		try {
			if( repository.remove(enterprise) ){
				addMessage("ui.mobile.enterprise.remove", enterprise.getCdEnterprise());
				enterprises = null;
				enterprise = null;
			}
			return "enterprise";
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public Enterprises getEnterprise() {
		return enterprise;
	}
	
	public void setEnterprise(Enterprises enterprise) {
		this.enterprise = enterprise;
	}
	
	public UsersGroups getUserGroup() {
		return userGroup;
	}
	
	public void setUserGroup(UsersGroups userGroup) {
		this.userGroup = userGroup;
	}
	
	public Users getUser() {
		return user;
	}
	
	public void setUser(Users user) {
		this.user = user;
	}
	
	public List<Enterprises> getEnterprises() {
		try {
			if (enterprises == null) {
				enterprises = repository.list();
			}
			return enterprises;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	@Override
	public String toForm() {
		return "enterpriseForm";
	}
	
	@Override
	public String toList() {
		enterprise = new Enterprises();
		return "enterprise";
	}

}
